require(['jquery', 'cookie', 'app', 'lay'], function ($) {
    layui.use(['element', 'layer'], function () {
        var layer = layui.layer;
        layer.load();
    });
    require(['vue'], function (Vue) {
        var user = new Vue({
            el: "#user",
            data: {
                list: "",
                nickName:"",
                stateName:"",
                num:"",
                th:{
                    uuid:"",
                    state:"",
                    stateName:"",
                    status:"",
                    statusName:"",
                    ipone:"",
                    emil:"",
                    nickName:"",
                    passWord:"",
                    methodTime:""
                }
            },
            created: function () {
                this.loadPage(0, 10);
                setTimeout(function () {
                    layui.use('element', function () {
                        var element = layui.element;
                        layer.closeAll('loading');
                    });
                },200);
            },
            methods: {
                //设置数值
                loadPage: function (begin, end) {
                    var jon = {};
                    jon['begin'] = begin;
                    jon['end'] = end;
                    //jon['navig'] = this.navig;
                    if (this.nickName != null && this.nickName != '') {
                        jon['nickName'] = this.nickName;
                    } else {
                        jon['nickName'] = "";
                    }

                    if (this.stateName != null && this.stateName != '') {
                        jon['stateName'] = this.stateName;
                    } else {
                        jon['stateName'] = "";
                    }

                    this.list = JSON.parse(playAjax("1555508458596", JSON.stringify(jon)))['data']['data'];
                    this.num = JSON.parse(playAjax("1555508458596", JSON.stringify(jon)))['data']['num'];

                },
                referXadmin: function () {
                    window.location.reload();
                },
                pre_home: function () {
                    $("#list").show();
                    $("#seach").show();
                    $("#add").hide();
                    $("#update").hide();
                    $("#xq").hide();
                },
                show_add:function () {
                    $("#list").hide();
                    $("#seach").hide();
                    $("#add").show();
                },
                show_update:function () {
                    if (!this.checkSure()) {
                        return;
                    }
                    var td = $(".zh").find("td");
                    var xv = ($(td[0]).text()) - 1;
                    this.th = this.list[xv];
                    $("#list").hide();
                    $("#seach").hide();
                    $("#update").show();
                    setTimeout(function () {
                        referFrom();
                    }, 0)
                },
                xz_state:function (e) {
                    var e=e.currentTarget;
                    layer.open({
                        type:2
                        ,content: "./vsp?code=101"
                        ,area:["600px","400px"]
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var body = layer.getChildFrame('body', index);
                            var w = $(layero).find("iframe")[0].contentWindow;
                            if(w.checkSure()){
                                $(e).val((w.checkSure())['roleName']);
                                $(e).attr("uuid",(w.checkSure())['roleCode']);
                                layer.closeAll();
                            }
                        }
                        ,btn2: function(index, layero){
                            layer.closeAll();
                        }
                        ,cancel: function(){
                            layer.closeAll();
                        }
                    });
                },
                deleted:function () {
                    if (!this.checkSure()) {
                        return;
                    }
                    layer.open({
                        content: '确定要删除吗？'
                        , btn: ['确认', '取消']
                        , yes: function (index, layero) {
                            var jon = {};
                            jon['uuid'] = user.checkSure();
                            var re = JSON.parse(playAjax("1555508458598", JSON.stringify(jon)));
                            alr(re['title']);
                            //按钮【按钮一】的回调
                            layer.closeAll();
                            user.referXadmin();
                        }
                        , btn2: function (index, layero) {
                            //按钮【按钮二】的回调
                            layer.closeAll();
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                        , cancel: function () {
                            //右上角关闭回调
                            layer.closeAll();
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });
                },
                checkSure: function () {
                    if ($(".zh").length <= 0) {
                        alr("未选中！请选择一条记录");
                        return false;
                    } else {
                        var td = $(".zh").find("td");
                        var uuid = $(td[1]).text();
                        return uuid;
                    }
                },
                show_xq:function () {
                    if (!this.checkSure()) {
                        return;
                    }
                    var td = $(".zh").find("td");
                    var xv = ($(td[0]).text()) - 1;
                    this.th = this.list[xv];
                    $("#list").hide();
                    $("#seach").hide();
                    $("#xq").show();
                }
            }
        });
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            var jon = {};
            //执行一个laypage实例
            laypage.render({
                elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
                , count: user.num//数据总数，从服务端得到
                , limit: 10
                , jump: function (obj, first) {
                    //obj包含了当前分页的所有参数，比如：
                    var x = obj.curr; //得到当前页，以便向服务端请求对应页的数据。
                    var y = obj.limit; //得到每页显示的条数
                    //首次不执行
                    if (!first) {
                        //do something
                        var begin = (x - 1) * y;
                        var end = y;
                        user.loadPage(begin, end);
                    }
                }
            });
        });
        layui.use('form', function () {
            var form = layui.form;
            form.render();
            form.on('submit(soso)', function (data) {
                user.nickName = data.field.nickName;
                user.stateName = data.field.stateName;
                user.loadPage(0, 10);
                return false;
            });
            form.on('submit(formDemo)',function (data) {
                var jon=data.field;
                if(!jon['status']){
                    jon['status']='1';
                    jon['statusName']='禁用'
                }else{
                    jon['status']='0'
                    jon['statusName']='启用'
                }
                if(jon['passWord']!=jon['passWord2']){
                    alr("两次输入密码不一致，请重新输入！");
                    return false;
                }
                if(jon['ipone']!=''&&jon['ipone']!=null){
                    if(!checkPhone(jon['ipone'])){
                        alr("请输入正确的手机号！");
                        return false;
                    }
                }
                if(jon['emil']!=''&&jon['emil']!=null){
                    if(!checkEmail(jon['emil'])){
                        alr("请输入正确的邮箱！");
                        return false;
                    }
                }
                var x=$('input[name="stateName"]');
                jon['state']=$(x[1]).attr("uuid");
                var re=JSON.parse(playAjax("1555508458597",JSON.stringify(jon)));
                alr(re['title']);
                if(re['code']=='0'){
                    setTimeout(function () {
                        user.referXadmin();
                    },500);
                }
                return false;
            });
            form.on('submit(formDemos)',function (data) {
                var jon=data.field;
                if(!jon['status']){
                    jon['status']='1';
                    jon['statusName']='禁用'
                }else{
                    jon['status']='0'
                    jon['statusName']='启用'
                }
                if(jon['passWord']!=jon['passWord2']){
                    alr("两次输入密码不一致，请重新输入！");
                    return false;
                }
                if(jon['ipone']!=''&&jon['ipone']!=null){
                    if(!checkPhone(jon['ipone'])){
                        alr("请输入正确的手机号！");
                        return false;
                    }
                }
                if(jon['emil']!=''&&jon['emil']!=null){
                    if(!checkEmail(jon['emil'])){
                        alr("请输入正确的邮箱！");
                        return false;
                    }
                }
                var x=$('input[name="stateName"]');
                jon['state']=$(x[1]).attr("uuid");
                jon['uuid']=user.th['uuid'];
                var re=JSON.parse(playAjax("1555508458599",JSON.stringify(jon)));
                alr(re['title']);
                if(re['code']=='0'){
                    setTimeout(function () {
                        user.referXadmin();
                    },500);
                }
                return false;
            })
        });

    })
});